﻿using SHZY;
using SHZY.Common.Options;
using SHZY.Extras.DatabaseAccessor.SqlSugar.Extensions;
using SqlSugar;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// SqlSugar配置拓展.
/// </summary>
public static class SqlSugarConfigureExtensions
{
    public static IServiceCollection SqlSugarConfigure(this IServiceCollection services)
    {
        // 获取选项
        ConnectionStringsOptions connectionStrings = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);

        List<ConnectionStringsOptions> McenterconnectionsList = App.GetConfig<List<ConnectionStringsOptions>>("ConnectionStrings", true);

        List<ConnectionConfig> connectConfigList = new List<ConnectionConfig>();

        string? connectionStr = connectionStrings.DefaultConnection;
        var dataBase = connectionStrings.DBName;
        var DBType = (DbType)Enum.Parse(typeof(DbType), connectionStrings.DBType);
        var ConfigId = connectionStrings.ConfigId;
        var DBName = connectionStrings.DBName;

        // 默认数据库
        connectConfigList.Add(new ConnectionConfig
        {
            ConnectionString = string.Format(connectionStr, DBName),
            DbType = DBType,
            IsAutoCloseConnection = true,
            ConfigId = ConfigId,
            InitKeyType = InitKeyType.Attribute,
            MoreSettings = new ConnMoreSettings()
            {
                IsAutoRemoveDataCache = true // 自动清理缓存
            },
        });

        foreach (var connectionConfig in McenterconnectionsList)
        {
            string? McenterconnectionStr = connectionConfig.DefaultConnection;
            var MdataBase = connectionConfig.DBName;
            var MDBType = (DbType)Enum.Parse(typeof(DbType), connectionConfig.DBType);
            var MConfigId = connectionConfig.ConfigId;
            var MDBName = connectionConfig.DBName;

            // 默认数据库
            connectConfigList.Add(new ConnectionConfig
            {
                ConnectionString = string.Format(McenterconnectionStr, MDBName),
                DbType = MDBType,
                IsAutoCloseConnection = true,
                ConfigId = MConfigId,
                InitKeyType = InitKeyType.Attribute,
                MoreSettings = new ConnMoreSettings()
                {
                    IsAutoRemoveDataCache = true // 自动清理缓存
                },
            });
        }

        services.AddSqlSugar(connectConfigList, db =>

        {
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                if (sql.StartsWith("SELECT"))
                    Console.ForegroundColor = ConsoleColor.Green;

                if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT"))
                    Console.ForegroundColor = ConsoleColor.White;

                if (sql.StartsWith("DELETE"))
                    Console.ForegroundColor = ConsoleColor.Blue;

                // 在控制台输出sql语句
                Console.WriteLine(SqlProfiler.ParameterFormat(sql, pars));
                Console.WriteLine();

                // 在MiniProfiler内显示
                // App.PrintToMiniProfiler("SqlSugar", "Info", SqlProfiler.ParameterFormat(sql, pars));
            };
        });

        return services;
    }
}